package com.cheung.leetcode.stackandqueue;

import org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.stackandqueue
 * @Author :  cheungming
 * @CreateTime : 2024-05-11 09:01:16
 * @Description : 删除字符串中相邻的重复项
 * @Version : 1.0
 */
public class Code1047Test {

    public String removeDuplicates(String s) {
        // 不需要 Stack<Character> stack = new Stack<>(); 这样使用真实的栈

        StringBuilder stack = new StringBuilder();
        int top = -1;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (top > -1 && stack.charAt(top) == c) {
                stack.deleteCharAt(top);
                top--;
            } else {
                stack.append(c);
                top++;
            }
        }

        return stack.toString();

    }


    @Test
    public void test1() {
        String s = "abbaca";
        assert "ca".equals(removeDuplicates(s));
    }
}
